﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using CF.Data.Entity.FrmMemberEntity;

namespace CF.Member.DAL.Member
{
    public class FM_MainMemberDAL
    {
        public const int PasswordLimit1 = 5;
        public const int PasswordLimit2 = 16;

        /// <summary>
        /// 身份验证
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool Verify(string userName, string password, out int uid)
        {
            bool result = false;
            uid = 0;
            string newPsw = password + PasswordSalt.AdminSalt;
            string hashPsw = FormsAuthentication.HashPasswordForStoringInConfigFile(newPsw, "md5");
            //MD5加密密码，然后取6～21个字符
            hashPsw = hashPsw.ToLower().Substring(PasswordLimit1, PasswordLimit2);
            caifendbEntities context = new caifendbEntities();
            FM_MainMember mmember = context.FM_MainMember.FirstOrDefault(c => c.MemberID == userName);
            if (mmember != null)
            {
                if (mmember.Password.Equals(hashPsw))
                {
                    uid = mmember.UID;
                    result = true;
                }
                
            }
            return result;
        }

        /// <summary>
        /// 记录登录信息，包括总登录次数、登录时间等。
        /// </summary>
        public void MarkLoginInfo(int uid, string loginIp)
        {
            caifendbEntities context = new caifendbEntities();
            FM_MainMember mmember = context.FM_MainMember.FirstOrDefault(c => c.UID == uid);
            if (mmember != null)
            {
                int count = mmember.LoginCount ?? 0;
                count++;
                mmember.LoginCount = count;
                mmember.LastLogin = DateTime.Now;
                mmember.LastLoginIP = loginIp;
                context.SaveChanges();
            }
        }

        public List<FM_MainMember> GetAllMember()
        {
            caifendbEntities context = new caifendbEntities();
            context.ContextOptions.LazyLoadingEnabled = false;
            List<FM_MainMember> memlist = new List<FM_MainMember>();
            memlist = context.FM_MainMember.ToList();
            return memlist;
        }

        public bool ChangePassword(string memberId, string newPsw)
        {
            bool result = false;
            caifendbEntities context = new caifendbEntities();
            FM_MainMember mmember = context.FM_MainMember.FirstOrDefault(c => c.MemberID == memberId);
            if (mmember != null)
            {
                string psw = newPsw + PasswordSalt.AdminSalt;
                string hashPsw = FormsAuthentication.HashPasswordForStoringInConfigFile(psw, "md5");
                //MD5加密密码，然后取6～21个字符
                hashPsw = hashPsw.ToLower().Substring(PasswordLimit1, PasswordLimit2);
                mmember.Password = hashPsw;
                context.SaveChanges();
                result = true;
            }
            return result;
        }
    }
}
